package com.sctvcloud.zhaohua_sd.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.ruihang.generalibrary.ui.util.OnItemInternalClick;
import com.ruihang.generalibrary.ui.widget.CustomEXImageView;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.ruihang.generalibrary.utils.DateUtils;
import com.sctvcloud.zhaohua_sd.R;
import com.sctvcloud.zhaohua_sd.application.Constances;
import com.sctvcloud.zhaohua_sd.base.BaseActivity;
import com.sctvcloud.zhaohua_sd.beans.HighRiseNewsInfo;
import com.sctvcloud.zhaohua_sd.beans.LiveItem;
import com.sctvcloud.zhaohua_sd.beans.NewsItem;
import com.sctvcloud.zhaohua_sd.beans.SearchDataBean;
import com.sctvcloud.zhaohua_sd.beans.TopicItem;
import com.sctvcloud.zhaohua_sd.ui.activities.HighRiseMoreActivity;
import com.sctvcloud.zhaohua_sd.ui.activities.LivingActivity;
import com.sctvcloud.zhaohua_sd.ui.activities.NewesDetailActivity;
import com.sctvcloud.zhaohua_sd.ui.activities.TopicDetailActivity;
import com.sctvcloud.zhaohua_sd.ui.util.IListShowData;
import com.sctvcloud.zhaohua_sd.ui.utils.GlideUtil;

import java.util.ArrayList;
import java.util.List;

public class HighRiseNewsListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public static final int NEWS_TYPE = 1;
    public static final int LOAD_MORE_TYPE = 2;

    private List<HighRiseNewsInfo> dataList;
    private Context ctx;

    public HighRiseNewsListAdapter(Context context, List<HighRiseNewsInfo> list) {
        this.ctx = context;
        this.dataList = list;
    }

    static class HighRiseNewsListItemHolder extends RecyclerView.ViewHolder {
        LinearLayout newsLayout;
        LinearLayout loadMoreLayout;
        CustomFontTextView notes;
        CustomFontTextView moreNews;
        CustomFontTextView newsTitle;
        CustomFontTextView newsTime;
        CustomEXImageView new_img;
        ImageView video_play;

        public HighRiseNewsListItemHolder(View view) {
            super(view);
            newsLayout = (LinearLayout) view.findViewById(R.id.news_layout);
            loadMoreLayout = (LinearLayout) view.findViewById(R.id.load_more_layout);
            notes = (CustomFontTextView) view.findViewById(R.id.notes);
            newsTitle = (CustomFontTextView) view.findViewById(R.id.new_title);
            newsTime = (CustomFontTextView) view.findViewById(R.id.new_time);
            moreNews = (CustomFontTextView) view.findViewById(R.id.more_news);
            new_img = (CustomEXImageView) view.findViewById(R.id.new_img);
            video_play = (ImageView) view.findViewById(R.id.video_play);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public int getItemCount() {
        if (null != dataList) {
            return dataList.size();
        }
        return 0;
    }

    public int setDataList(List<HighRiseNewsInfo> list) {
        int pos = getItemCount();
        this.dataList = list;
        if (pos > 0) {
            notifyItemChanged(pos - 1);
        }
        return getItemCount() - 1;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        View view = LayoutInflater.from(ctx).inflate(R.layout.high_rise_news_list_item, parent, false);
        holder = new HighRiseNewsListItemHolder(view);
        return holder;
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (null != dataList && dataList.size() > position) {
            final HighRiseNewsInfo data = dataList.get(position);
            HighRiseNewsListItemHolder h = (HighRiseNewsListItemHolder) holder;
            h.newsLayout.setVisibility(View.GONE);
            h.loadMoreLayout.setVisibility(View.GONE);
            h.video_play.setVisibility(View.GONE);
            if (data.getType() == NEWS_TYPE) {
                h.newsLayout.setVisibility(View.VISIBLE);
                final IListShowData itemData = data.getItemData();
                if (null != itemData) {
                    if (itemData instanceof NewsItem) {
                        NewsItem item = (NewsItem) itemData;
                        h.newsTitle.setText(item.getNewsTitle());
                        h.newsTime.setText( DateUtils.formatDate(DateUtils.getDataDate(item), ""));
//                        h.newsTime.setText(item.getTime());
                        GlideUtil.getGlideWithMid169Def(ctx, item.getNewsImage()).into(h.new_img);
                        if (item.getNewsType() == 3) {
                            h.video_play.setVisibility(View.VISIBLE);
                            h.video_play.setImageResource(R.mipmap.icon_home_play);
                        } else if (item.getNewsType() == 2) {
                            h.video_play.setVisibility(View.VISIBLE);
                            h.video_play.setImageResource(R.mipmap.icon_home_voice);
                        }
                    } else if (itemData instanceof LiveItem) {
                        LiveItem item = (LiveItem) itemData;
                        h.newsTitle.setText(item.getTitle());
//                        h.newsTime.setText(item.getTime());
                        h.newsTime.setText( DateUtils.formatDate(DateUtils.getDataDate(item), ""));
                        h.video_play.setVisibility(View.VISIBLE);
                        h.video_play.setImageResource(R.mipmap.icon_home_play);
                        GlideUtil.getGlideWithMid169Def(ctx, item.getLiveImg()).into(h.new_img);
                    } else if (itemData instanceof TopicItem) {
                        TopicItem item = (TopicItem) itemData;
                        h.newsTitle.setText(item.getNewsTitle());
//                        h.newsTime.setText(item.getTime());
                        h.newsTime.setText( DateUtils.formatDate(DateUtils.getDataDate(item), ""));
                        GlideUtil.getGlideWithMid169Def(ctx, item.getNewsImage()).into(h.new_img);
                    }
                }
                h.newsLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (null != itemData) {
                            Intent intent = null;
                            if (itemData instanceof NewsItem) {
                                NewsItem item = (NewsItem) itemData;
                                intent = new Intent(ctx, NewesDetailActivity.class);
                                intent.putExtra(BaseActivity.EX_URL, item.getNewsUrl());
                            } else if (itemData instanceof LiveItem) {
                                LiveItem item = (LiveItem) itemData;
                                intent = new Intent(ctx, LivingActivity.class);
                                intent.putExtra(BaseActivity.EX_URL, item.getLiveUrl());
                                intent.putExtra(BaseActivity.EX_DATA, item.getLiveImg());
                            } else if (itemData instanceof TopicItem) {
                                TopicItem item = (TopicItem) itemData;
                                intent = new Intent(ctx, TopicDetailActivity.class);
                                intent.putExtra(BaseActivity.EX_URL, item.getNewsUrl());
                            }
                            if (null != intent) {
                                intent.putExtra(BaseActivity.EX_COLUMN_TYPE, HomePagerAdapter.TYPE_HIGH_RISE);
                                ctx.startActivity(intent);
                            }
                        }
                    }
                });
            } else if (data.getType() == LOAD_MORE_TYPE) {
                h.loadMoreLayout.setVisibility(View.VISIBLE);
                h.moreNews.setText("查看更多");
                h.loadMoreLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(ctx, HighRiseMoreActivity.class);
                        intent.putExtra(HighRiseMoreActivity.HIGH_RISE_MORE, data.getMoreUrl());
                        ctx.startActivity(intent);
                    }
                });
            }
        }
    }

}
